package com.NetWork.lesson3;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

//消息接送方
public class UdpReceive {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(7777);

        //准备接受packet
        byte[] buffer = new byte[1024];
        while (true){
            DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
            socket.receive(packet);//阻塞式接受包裹
            //断开连接
            byte[] data = packet.getData();
            String receivedata = new String(data, 0, data.length);
            System.out.println(receivedata);
            if (receivedata.equals("q")){
                break;
            }
        }

        socket.close();
    }
}

    